package order.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.*;
import com.example.utils.*;
import com.example.entity.ShippingOrder;
import order.client.CarrierClient;
import order.client.UserClient;
import order.mapper.OperationRecordMapper;
import order.mapper.ShippingOrderMapper;
import order.mapper.TruckOrderMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ShippingOrderService {
    @Resource
    ShippingOrderMapper shippingOrderMapper;
    @Resource
    OperationRecordMapper operationRecordMapper;
    @Resource
    TruckOrderMapper truckOrderMapper;
    @Resource
    UserClient userClient;
    @Resource
    CarrierClient carrierClient;

    public List<ShippingOrder> getShippingOrder(Long creatorId, Integer state, Long carrierId) {
        QueryWrapper wrapper = new QueryWrapper();
        if(creatorId!=null) wrapper.eq("creator_id", creatorId);
        if(state!=null) wrapper.eq("state", state);
        if(carrierId!=null) wrapper.eq("carrier_id", carrierId);
        List<ShippingOrder> shippingOrder = shippingOrderMapper.selectList(wrapper);
        shippingOrder.stream().map(s -> moreOrderDetail(s));
        return shippingOrder;
    }

    private ShippingOrder moreOrderDetail(ShippingOrder s){
        List<OperationRecord> sl = operationRecordMapper.selectList(new QueryWrapper<OperationRecord>().eq("type",1).eq("order_id",s.getId()));
        List<TruckOrder> truckOrder = truckOrderMapper.selectList(new QueryWrapper<TruckOrder>().eq("order_id",s.getId()));
        for(TruckOrder t:truckOrder){
            User driver = userClient.selectById(t.getDriverId());
            Truck truck = carrierClient.selectTruckById(t.getTruckId());
            t.setDriver(driver.getRealName());
            String truckName = truck.getNumber()+"(dwt:"+truck.getWeight()+")";
            t.setTruck(truckName);
            List<OperationRecord> tl = operationRecordMapper.selectList(new QueryWrapper<OperationRecord>().eq("type",2).eq("order_id",t.getId()));
            t.setOperationRecords(tl);
        }
        s.setOperationRecords(sl);
        s.setTruckOrder(truckOrder);
        return s;
    }


    public void createOrder(ShippingOrder s) {
        User u = userClient.selectById(s.getCreatorId());
        s.setTime(new Date());
        s.setCreatorName(u.getRealName());
        shippingOrderMapper.insert(s);

    }

    public void delete(Long id) {
        shippingOrderMapper.deleteById(id);
    }
    public ShippingOrder findShippingOrder(Long orderId) {
        ShippingOrder s = shippingOrderMapper.selectById(orderId);
        return moreOrderDetail(s);
    }

    public String changeState(Long carrierId,Integer state, Long id, Long operator) {
        ShippingOrder s = shippingOrderMapper.selectById(id);
        String res = null;
        switch (state) {
            case 1:
                s.setState(2);
                s.setCarrierId(carrierId);
                s.setLogisticsCost((double) (carrierClient.getUnitPrice(carrierId)*s.getDistance()*s.getWeight()));
                res = "已接单";
                break;
            case 8:
                s.setState(7);
                s = settle(s);
                res = "已签收";
                break;
            case 9:
                s.setState(8);
                res = "结算完成";
        }
        shippingOrderMapper.updateById(s);
        OperationRecord task = new OperationRecord(1,id,state,operator,new Date());
        operationRecordMapper.insert(task);
        return res;
    }

    private ShippingOrder settle(ShippingOrder s) {
        List<TruckOrder> tl = truckOrderMapper.selectList(new QueryWrapper<TruckOrder>().eq("order_id",s.getId()));
        double unexpectedCost = 0;
        for(TruckOrder t:tl){
            unexpectedCost+=t.getUnexpectedCost();
        }
        s.setUnexpectedCost(unexpectedCost);
        s.setTotalPrice(s.getLogisticsCost()+unexpectedCost);
        return s;
    }

    public void createTruckOrder(TruckOrder t){
        //插入派车单
        t.setCreateTime(new Date());
        truckOrderMapper.insert(t);
        ShippingOrder s = shippingOrderMapper.selectById(t.getOrderId());
        //更新货车和司机状态
        Truck truck = carrierClient.selectTruckById(t.getTruckId());
        truck.setState(1);
        CarrierDriver cd = new CarrierDriver(t.getCarrierId(),t.getDriverId());
        cd.setState(1);
        carrierClient.updateTruckById(truck);
        carrierClient.updateDriverState(1,t.getCarrierId(),t.getDriverId());
        //更新货运单状态
        List<TruckOrder> truckOrders = truckOrderMapper.selectList(new QueryWrapper<TruckOrder>().eq("order_id",s.getId()));
        Float weight = 0F;
        for (TruckOrder item:truckOrders){
            weight+=item.getWeight();
        }
        if (weight>=s.getWeight()){
            s.setState(3);
            shippingOrderMapper.updateById(s);
        }
        //操作记录
        OperationRecord task = new OperationRecord(1,t.getOrderId(),2,t.getCarrierId(),new Date());
        operationRecordMapper.insert(task);
    }
}
